Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming


THIS-OBJECT system reference

THIS-OBJECT is a system reference available from within a class definition. At run time, it returns the currently running instance of the class as an object reference. Its most important purpose is to allow a method of the class to pass a reference to the currently instantiated object as a parameter or to return a reference to itself as a method return value.

The following example shows a class, acme.myObjs.NewCustomer, that inherits from the sample class acme.myObjs.CustObj. This is not one of the documented sample classes, but is created to illustrate a use of the THIS-OBJECT system reference. A call to its setNewCustObj( ) method passes an object reference to the current instance of itself (THIS-OBJECT) to the ListDate( ) method of the sample class acme.myObjs.Common.HelperClass, which timestamps itself and displays the resulting timestamp value, as shown:

CLASS acme.myObjs.NewCustomer INHERITS acme.myObjs.CustObj:  
  
    DEFINE PRIVATE VARIABLE rHelperClass 
        AS CLASS acme.myObjs.Common.HelperClass NO-UNDO. 
    CONSTRUCTOR PUBLIC NewCustomer( ): 
        /* Create an instance of the HelperClass class */ 
        rHelperClass = NEW acme.myObjs.Common.HelperClass( ). 
    END CONSTRUCTOR 
    METHOD PUBLIC VOID setNewCustObj( ): 
        rHelperClass:ListDate(INPUT THIS-OBJECT). 
        MESSAGE THIS-OBJECT:timestamp.  
    END METHOD. 
    ... 
END CLASS. 

Because the THIS-OBJECT input parameter also represents a subclass of the sample class acme.myObjs.Common.CommonObj, the ListDate( ) method can invoke the updateTimestamp( ) method on the parameter object reference (rObject) to timestamp the object, which THIS-OBJECT can, in turn, reference when ListDate( ) returns, as previously:

CLASS acme.myObjs.Common.HelperClass: 
    ... 
    METHOD PUBLIC VOID ListDate  
            (INPUT rObject AS CLASS acme.myObjs.Common.CommonObj): 
        /* Timestamp this object */ 
        IF VALID-OBJECT(rObject) THEN 
            rObject:updateTimestamp( ). 
        ELSE 
            rError:Alert("Not a valid object"). 
    END METHOD. 
    ... 
END CLASS. 


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095